<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Predikat extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_predikat');
		$this->load->library('Excel_generator');
	}
	
	function index(){
		$this->load->view('master/predikat');
	}
	
	function grid(){
		$data = $this->mdl_predikat->getdata();
		echo $this->mdl_predikat->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['id_predikat'] = '';
		$data['kd_pt'] = '';
		$data['Nama'] = '';
		$data['IPKMin'] = '';
		$data['IPKMax'] = '';
		$data['Keterangan'] = '';
		$data['status'] = 'A';
		$this->load->view('master/predikat_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_predikat->getdataedit($kode);
		
		$data['id_predikat'] = $kode;
		$data['kd_pt'] = $r->row()->kd_pt;
		$data['Nama'] = $r->row()->Nama;
		$data['IPKMin'] = $r->row()->IPKMin;
		$data['IPKMax'] = $r->row()->IPKMax;
		$data['Keterangan'] = $r->row()->Keterangan;
		$data['status'] = $r->row()->status;
		
		$this->load->view('master/predikat_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("kd_pt", 'Universitas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("Nama", 'Nama Predikat', 'trim|required|xss_clean');
		$this->form_validation->set_rules("IPKMin", 'IPK Minimal', 'trim|required|xss_clean');
		$this->form_validation->set_rules("IPKMax", 'IPK Maximal', 'trim|required|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				$result = $this->mdl_predikat->InsertOnDb($data);
			}else { // edit
				$result=$this->mdl_predikat->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_predikat->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
	/*function pdf(){
		# inisialisasi library
		$this->load->library('our_pdf');
		$this->our_pdf->FPDF('L', 'mm', 'A4');
		define('FPDF_FONTPATH',APPPATH."libraries/fpdf/font/");
		
		// ambil data dari tabel
		$pdfdata = $this->mdl_predikat->get_pdf();
		if (count($pdfdata['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		}
		
		# create pdf
		$this->our_pdf->Open();
		$this->our_pdf->addPage();
		
		// judul laporan
		$this->our_pdf->setFont('arial','B',16);
		$posY = 35;
		$posX = 10;
		$midX = 150;
		$title = 'Data Predikat';
		$textPosX = $midX - ($this->our_pdf->GetStringWidth($title) / 2);
		$this->our_pdf->text($textPosX,$posY,$title);
		
		# setting coloumn
		$this->our_pdf->setFont('Arial','B',9);
		$this->our_pdf->setFillColor(255,255,255);
		$posY += 6;
		$this->our_pdf->setXY($posX,$posY);
		
		// init column
		$col_width = array();
		$col_align = array();
		$col_caption = array();
		$col_val_align = array();
		foreach($pdfdata['column'] as $col){
			array_push($col_width, 		$col['width']);
			array_push($col_align, 		$col['align']);
			array_push($col_caption, 	$col['caption']);
			array_push($col_val_align, 	$col['val_align']);
		}
		
		// set column option
		$this->our_pdf->SetWidths($col_width);
		$this->our_pdf->SetAligns($col_align);
		$this->our_pdf->Row($col_caption);
		
		# setting row
		$posY = 39;
		$this->our_pdf->setFont('arial','',9);	
		$this->our_pdf->setFillColor(255,255,255);
		
		$this->our_pdf->SetAligns($col_val_align);
		
		// insert record
		foreach($pdfdata['row'] as $row){
			$this->our_pdf->Row($row);
		}
		
		// finish
		$this->our_pdf->AliasNbPages();
		$this->our_pdf->Output("Data Predikat.pdf", "I");
	}*/
	
	function laporan_pdf(){
		 $this->load->library('HTML2PDF');
		 $html2pdf = new HTML2PDF('P', 'A4', 'fr');
		 $html2pdf->setDefaultFont('Arial');
		
		//filter
		#get filter
		//$fil['kd_prodi'] = $kd_prodi;
		
		  //$data['nama'] = '';
		 $data['namaUniv'] = 'STMIK BANDUNG';
		 $data['alamatUniv'] = 'Jl.Phh.Mustofa No. 39. Grand Surapati Core (SUCORE) Blok M No.19, Telp.022 - 7207777';
		 $data['kotaUniv'] = 'Bandung, Jawa Barat';
		 // ambil data dari tabel
		$data['data_pdf'] = $this->mdl_predikat->get_pdf();
		
		
		/* if (count($da['row'])==0){
			echo "Data Tidak Tersedia";
			return;
		} */
		
		
		 $konten = $this->load->view('master/predikat_laporan',$data,true);
		
         $html2pdf->writeHTML($konten, false);
        
		 $html2pdf->Output('predikat.pdf');
	}
	
	function laporan_excel() {			
		$query = $this->db->get('ref_predikat');
			$this->excel_generator->set_query($query);
			$this->excel_generator->set_header(array('ID Predikat','Kode Universitas', 'Nama', 'IPK Minimal','IPK Maksimal', 'Keterangan','Status'));
			$this->excel_generator->set_column(array('id_predikat','kd_pt','Nama', 'IPKMin', 'IPKMax','Keterangan','status'));
			$this->excel_generator->set_width(array(12, 17, 25, 14, 14, 30, 5));
			$this->excel_generator->exportTo2007('Predikat');
	}
}